﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL.BaseDictionaryService;
using Model;
using BLL.BudgetService;

namespace FamilyManager
{
    public partial class MonthBudgetInputForm : Form
    {
        private MonthBudgetInputForm()
        {
            InitializeComponent();
        }

        private static MonthBudgetInputForm insance;

        public static MonthBudgetInputForm CreateInstance()
        {
            if (insance == null)
            {
                insance = new MonthBudgetInputForm();
            }
            return insance;
        }

        private void MonthBudgetInputForm_Load(object sender, EventArgs e)
        {
            dtpUseMonth.Text = dtpUseMonth.Value.AddMonths(1).ToString();
            BindGrid();
        }


        protected void BindGrid()
        {
            gvBudget.AutoGenerateColumns = false;
            gvBudget.Columns.Clear();

            DateTime dtSelect = dtpUseMonth.Value;

            DateTime beginDate=new DateTime(dtSelect.Year,dtSelect.Month,1);
            DateTime endDate = new DateTime(dtSelect.Year, dtSelect.Month, 30);
            QueryBudgetByDateTransaction objQueryBudgetByDateTransaction = new QueryBudgetByDateTransaction(beginDate, endDate);
            gvBudget.DataSource = objQueryBudgetByDateTransaction.Query();

            DataGridViewTextBoxColumn dcOID = new DataGridViewTextBoxColumn();
            dcOID.HeaderText = "";
            dcOID.DataPropertyName = "Db_OID";
            gvBudget.Columns.Add(dcOID);
            dcOID.Visible = false;

            DataGridViewTextBoxColumn dcPayCategory = new DataGridViewTextBoxColumn();
            dcPayCategory.HeaderText = "预算项目";
            dcPayCategory.DataPropertyName = "PayCategory";
            gvBudget.Columns.Add(dcPayCategory);

            DataGridViewTextBoxColumn dcDb_BudgetSum = new DataGridViewTextBoxColumn();
            dcDb_BudgetSum.HeaderText = "预算金额";
            dcDb_BudgetSum.DataPropertyName = "Db_BudgetSum";
            gvBudget.Columns.Add(dcDb_BudgetSum);



            DataGridViewTextBoxColumn dcDb_PayCategory = new DataGridViewTextBoxColumn();
            dcDb_PayCategory.HeaderText = "";
            dcDb_PayCategory.DataPropertyName = "Db_PayCategory";
            gvBudget.Columns.Add(dcDb_PayCategory);
            dcDb_PayCategory.Visible = false;
        }


        /// <summary>
        /// 初始化预算单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInit_Click(object sender, EventArgs e)
        {
            
            //if()

            DateTime dtSelect = dtpUseMonth.Value;

            DateTime beginDate=new DateTime(dtSelect.Year,dtSelect.Month,1);
            DateTime endDate = new DateTime(dtSelect.Year, dtSelect.Month, 30);
            CheckMonthBudgetExistTransaction objCheckMonthBudgetExistTransaction = new CheckMonthBudgetExistTransaction(beginDate.Year, beginDate.Month);
            if (!objCheckMonthBudgetExistTransaction.IsExist)
            {
                QueryPayCategoryTransaction objQueryPayCategoryTransaction = new QueryPayCategoryTransaction();
                objQueryPayCategoryTransaction.Execute();
                IList<D_PayCategoryObject> payCategorys = objQueryPayCategoryTransaction.PayCategorys;
                AddBudgetTransaction objAddBudgetTransaction = new AddBudgetTransaction();
                foreach (D_PayCategoryObject item in payCategorys)
                {
                    T_BudgetObject objBudget = new T_BudgetObject();
                    objBudget.Db_BeginDate = beginDate;
                    objBudget.Db_EndDate = endDate;
                    objBudget.Db_PayCategory = item.Db_OID;
                    objBudget.Db_ReferValue = 0;
                    objBudget.Db_BudgetSum = 0;
                    objAddBudgetTransaction.BudgetObject = objBudget;
                    objAddBudgetTransaction.Execute();
                }
                BindGrid();
            }
            else
            {
                MessageBox.Show("无需初始化，请直接编辑当前选择中的月份的预算","提示");
            }
        }

        private void dtpUseMonth_ValueChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 保存预算结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            DateTime dtSelect = dtpUseMonth.Value;

            DateTime beginDate = new DateTime(dtSelect.Year, dtSelect.Month, 1);
            DateTime endDate = new DateTime(dtSelect.Year, dtSelect.Month, 30);

            EditBudgetTransaction objEditBudgetTransaction = new EditBudgetTransaction();
            foreach (DataGridViewRow item in gvBudget.Rows)
            {
                T_BudgetObject obj = new T_BudgetObject();
                obj.Db_OID = int.Parse(item.Cells[0].Value.ToString());
                obj.Db_BeginDate = beginDate;
                obj.Db_EndDate = endDate;
                obj.Db_BudgetSum = decimal.Parse(item.Cells[2].Value.ToString());
                obj.Db_PayCategory = int.Parse(item.Cells[3].Value.ToString());
                objEditBudgetTransaction.NewBudgetObject = obj;
                
                objEditBudgetTransaction.Execute();
            }
            BindGrid();
        }
    }
}
